<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>类型限制简写</title>
</head>

<body>
    <div id="test"></div>
    <div id="test1"></div>
    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 引入prop-types,用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>
    <script type="text/babel">
        //1 创建类式组件
        class Person extends React.Component {
            //可不写
            constructor(props){
                //构造器是否接收props，是否传递给super,
                //取决于:是否希望在构造器中通过this访问props
                super(props)
                console.log('constructor',this.props)
            }
            render() {
                const { name, age, sex } = this.props
                return (
                    <ul>
                        <li>姓名：{name}</li>
                        <li>性别：{sex}</li>
                        <li>年龄：{age + 1}</li>
                    </ul>
                )
            }
            //进行类型和必要性
            static propTypes = {
                name: PropTypes.string.isRequired,
                sex: PropTypes.string,
                age: PropTypes.number,
                //函数
                speak: PropTypes.func
            }
            //默认值
            static defaultProps = {
                sex: '男',
                age: 0
            }
        }

        //2 渲染组件到页面
        ReactDOM.render(<Person name='小李' sex="女" age={12} />, document.getElementById('test'))
        ReactDOM.render(<Person name='小李' sex="女" />, document.getElementById('test1'))
    </script>
</body>

</html>